package com.shequan.search.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ailk.dazzle.util.data.JSONUtils;
import com.ailk.dazzle.util.type.DateUtils;
import com.shequan.common.bean.ResultDataStruct;
import com.shequan.common.bean.ViewTextConstans;
import com.shequan.search.bean.bo.SearchActivityBean;
import com.shequan.search.bean.bo.SearchUserInfoBean;
import com.shequan.search.service.SearchService;
import com.shequan.shequan.bean.pojo.Shequan;

/**
 * 
 * <P>
 * Copyright 2012 shequaner. 搜索模块 All Rights Reserved.
 * 
 * @author mazg
 * @version 1.0, 2012-3-1
 * @see com.shequan.search.controller.SearchController
 * @since 1.0
 */
@Controller
@RequestMapping("/search")
public class SearchController {
	private static final Logger log = Logger.getLogger(SearchController.class);
	private SearchService searchService;

	@RequestMapping(value = "/shequan")
	public void searchShequan(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultDataStruct<List<Shequan>> rds = new ResultDataStruct<List<Shequan>>();
		String shequanName;
		String conUserName;
		String createStartDate;
		String createEndDate;
		String schoolName;
		try {
			shequanName = request.getParameter("shequanName");
			conUserName = request.getParameter("conUserName");
			createStartDate = request.getParameter("createStartDate");
			createEndDate = request.getParameter("createEndDate");
			schoolName = request.getParameter("schoolName");
			Date startDate = null;
			Date endDate = null;
			if (createStartDate != null && !"".equals(createStartDate)) {
				startDate = DateUtils.string2Date(createStartDate, 7);
				endDate = DateUtils.string2Date(createEndDate, 7);
			}
			List<Shequan> shequanList = searchService.searchShequanList(shequanName,conUserName,schoolName,startDate,endDate);
			rds.setData(shequanList);
			rds.setBusiSuccess(true);
			rds.setSuccess(true);
		} catch (Exception e) {
			log.error(e);
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		log.debug(jsonData);
		response.getWriter().print(jsonData);
	}

	@RequestMapping(value = "/activity")
	public void searchActivity(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultDataStruct<List<SearchActivityBean>> rds = new ResultDataStruct<List<SearchActivityBean>>();
		String activityName;
		String shequanName;
		Date startTime = null;
		String activityPlace;
		String schoolName;
		try {
			activityName = request.getParameter("activityName");
			shequanName = request.getParameter("shequanName");
			String startTimeStr = request.getParameter("startTime");
			if (startTimeStr != null && !"".equals(startTimeStr)) {
				startTime = DateUtils.string2Date(startTimeStr, 6);
			}
			activityPlace = request.getParameter("activityPlace");
			schoolName = request.getParameter("schoolName");
			List<SearchActivityBean> searchActivityList = searchService.queryActivityList(activityName, shequanName, startTime, activityPlace, schoolName);
			rds.setData(searchActivityList);
			rds.setBusiSuccess(true);
			rds.setSuccess(true);
		} catch (Exception e) {
			log.error(e);
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		log.debug(jsonData);
		response.getWriter().print(jsonData);
	}

	@RequestMapping(value = "/friend")
	public void searchFriend(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultDataStruct<List<SearchUserInfoBean>> rds = new ResultDataStruct<List<SearchUserInfoBean>>();
		String user;
		String area;
		String school;
		String gender;
		Integer userGender = null;
		try {
			user = request.getParameter("userName");
			area = request.getParameter("areaName");
			school = request.getParameter("schoolName");
			gender = request.getParameter("userGender");
			if (gender != null && !"".equals(gender)) {
				userGender = Integer.parseInt(gender);
			}
			List<SearchUserInfoBean> userInfoList = searchService.searchFriendList(user, area, school, userGender);
			rds.setData(userInfoList);
			rds.setSuccess(true);
			rds.setBusiSuccess(true);
		} catch (Exception e) {
			log.error(e);
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.UNKNOW_ERROR_MSG);
		}
		String jsonData = JSONUtils.bean2JsonString(rds);
		log.debug(jsonData);
		response.getWriter().print(jsonData);
	}

	/**
	 * @param searchService
	 *            the searchService to set
	 */
	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

}
